//
//  MineMatrixController.swift
//  SCA
//
//  Created by tjun on 2025/9/21.
//

import UIKit

class MineMatrixController: BaseController {

    private var scrollView: UIScrollView!
    private var contentView: UIView!
    private var lastView: UIView!
    private var datas: [MatrixNetworkModel] = []

    private let centerLabel = UILabel(font: UIFontMake(14), textColor: .white, textAlignment: .center)

    override func viewDidLoad() {
        super.viewDidLoad()
        
        scrollView = UIScrollView(frame: CGRect(x: 0, y: navBarHeight, width: view.width, height: screenHeight - navBarHeight)).addTo(view)
        scrollView.keyboardDismissMode = .onDrag
        scrollView.showsVerticalScrollIndicator = false
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.alwaysBounceVertical = true
        scrollView.contentInsetAdjustmentBehavior = .never
        
        let contentSize = CGSize(width: scrollView.width, height: scrollView.height)
        
        scrollView.contentSize = contentSize
        
        contentView = UIView().addTo(scrollView)
        contentView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
            make.size.equalTo(contentSize)
        }
        
        let topMineMatrixView = MineMatrixContentView().addTo(contentView)
        topMineMatrixView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(20)
        }
        
        
        let centerView = UIImageView()
        centerView.addTo(contentView)
        centerView.image = UIImage(named: "referralNetwork_bg")
        centerView.snp.makeConstraints { make in
            make.width.height.equalTo(50)
            make.centerX.equalToSuperview()
            make.top.equalTo(topMineMatrixView.snp.bottom).offset(20)
        }
        
        centerLabel.addTo(centerView)
        centerLabel.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        let bottomMatrixView = MineMatrixContentView().addTo(contentView)
        bottomMatrixView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(centerView.snp.bottom).offset(20)
        }

        lastView = bottomMatrixView
        updateContentSize()
        
        loadData()
    }
    
    private func loadData() {
        MineService.loadMatrixData(success: { models in
            self.datas.removeAll()
            self.datas.append(contentsOf: models)
            
            if let index = self.datas.firstIndex(where: { $0.isMain }) {
                let firstModel = self.datas.remove(at: index)
                self.centerLabel.text = firstModel.powerDisp
            }
        })
    }
 
    private func updateContentSize() {
        
        contentView.setNeedsLayout()
        contentView.layoutIfNeeded()
        
        let contentSize = CGSizeMake(view.width, lastView.maxY + 20 + tabBarSafeBottomMargin)
        scrollView.contentSize = contentSize
        contentView.snp.remakeConstraints { make in
            make.edges.equalToSuperview()
            make.size.equalTo(contentSize)
        }
    }

}
